package cn.xwt.weidisk.controller.system;


import cn.xwt.weidisk.constant.ErrorCode;
import cn.xwt.weidisk.pojo.dao.CategoryDo;
import cn.xwt.weidisk.pojo.dao.LinkDo;
import cn.xwt.weidisk.pojo.dto.CategoryData;
import cn.xwt.weidisk.pojo.dto.Resp;
import cn.xwt.weidisk.service.CategoryService;
import cn.xwt.weidisk.service.LinkService;
import cn.xwt.weidisk.util.CommonUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 薛慰涛
 */
@RestController
@RequestMapping("/system/collection")
public class CollectionController {

    @Resource
    CategoryService categoryService;

    @Resource
    LinkService linkService;

    final String ID = "id";

    /**
     * 新建分类
     * @param category 分类信息
     * @return resp
     */
    @PostMapping("/new-category")
    public Resp saveCategory(@RequestBody CategoryDo category) {
        category.setUid(CommonUtils.getUid());
        switch (categoryService.insertCategory(category)) {
            case CATEGORY_EXISTS:
                return new Resp(ErrorCode.USER_UPLOAD_FILE_EXCEPTION.getCode(), "分类已存在");
            case SUCCESS:
                return Resp.success();
            default:
                return Resp.serverError();
        }
    }

    /**
     * 获取分类列表
     * @return List<CategoryDo>
     */
    @GetMapping("/get-category-list")
    public List<CategoryData> getCategoryList() {
        List<CategoryData> categoryList = new LinkedList<>();
        for (CategoryDo item : categoryService.selectCategoryList(CommonUtils.getUid())) {
            Long quantity = linkService.count(item.getId());
            CategoryData categoryData = new CategoryData(item, quantity);
            categoryList.add(categoryData);
        }
        return categoryList;
    }

    /**
     * 删除分类
     * @param maps id
     * @return resp
     */
    @DeleteMapping("/delete-category")
    public Resp deleteCategory(@RequestBody Map<String, Long> maps) {
        Long id = maps.get(ID);
        Long uid = CommonUtils.getUid();
        if (categoryService.isCurrentUser(id, uid)) {
            LinkDo link = new LinkDo();
            link.setCid(id);
            linkService.deleteLink(link);
            categoryService.deleteCategory(id);
            return Resp.success();
        } else {
            return new Resp(ErrorCode.USER_REQUEST_PARAMETER_ERROR.getCode(), "没有分类操作权限");
        }
    }

    /**
     * 重命名category
     * @param category id，name
     * @return Resp
     */
    @PutMapping("/rename-category")
    public Resp renameCategory(@RequestBody CategoryDo category) {
        category.setUid(CommonUtils.getUid());
        categoryService.renameCategory(category);
        return Resp.success();
    }

    /**
     * 通过分类信息获取链接列表
     * @param cid 分类id
     * @return List<LinkDo>
     */
    @GetMapping("/get-link-list")
    public List<LinkDo> getLinkList(@RequestParam Long cid) {
        // 判断category是否属于当前用户
        return linkService.selectLinkList(cid, CommonUtils.getUid());
    }


    /**
     * 新建链接
     * @param linkDo 链接信息
     * @return Resp
     */
    @PostMapping("/new-link")
    public Resp newLink(@RequestBody LinkDo linkDo) {
        linkDo.setUid(CommonUtils.getUid());
        if (linkService.insertLink(linkDo)) {
            return Resp.success();
        } else {
            return Resp.serverError();
        }
    }

    /**
     * 删除链接
     * @param maps id
     * @return Resp
     */
    @DeleteMapping("/delete-link")
    public Resp deleteLink(@RequestBody Map<String, Long> maps) {
        LinkDo link = new LinkDo();
        link.setId(maps.get(ID));
        link.setUid(CommonUtils.getUid());
        linkService.deleteLink(link);
        return Resp.success();
    }

    /**
     * 修改链接信息
     * @param link 链接信息
     * @return resp
     */
    @PutMapping("/edit-link")
    public Resp editLink(@RequestBody LinkDo link) {
        link.setUid(CommonUtils.getUid());
        linkService.updateLink(link);
        return Resp.success();
    }
}
